package com.hunanwenli.tool;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolService {
    private static final int DEFAULT_CORE_SIZE=5;
    private static final int MAX_QUEUE_SIZE=10;
    private volatile static ThreadPoolExecutor executor;

    private ThreadPoolService() {};


    public static ThreadPoolExecutor getInstance() {
        if (executor == null) {
            synchronized (ThreadPoolService.class) {
                if (executor == null) {
                    executor = new ThreadPoolExecutor(
                    DEFAULT_CORE_SIZE,
                    Integer.MAX_VALUE,
                   5,
                    TimeUnit.MILLISECONDS,
                    new LinkedBlockingDeque<Runnable>(MAX_QUEUE_SIZE),
                    Executors.defaultThreadFactory(),
                    new ThreadPoolExecutor.AbortPolicy()
                    );
                }
            }
        }
        return executor;
    }


}